package com.ssmk;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.blankj.utilcode.util.SPUtils;
import com.ssmk.adapter.WelcomeAdapter;
import com.ssmk.home.HomeActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WelcomeActivity extends AppCompatActivity {
    @BindView(R.id.vp)
    ViewPager mVp;


    private SPUtils sputils = new SPUtils("fristopen");
    private Handler mhandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            setIntent();
        }
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        ButterKnife.bind(this);

        getspDate();

    }

    private void getspDate() {
        boolean mFrist = sputils.getBoolean("frist", false);
        if (mFrist) {
            setIntent();
        } else {
            sputils.put("frist", true);

            List<View> list = new ArrayList<>();
            View view1 = View.inflate(this,R.layout.view1,null);
            View view2 = View.inflate(this,R.layout.view2,null);
            View view3 = View.inflate(this,R.layout.view3,null);
            View view4 = View.inflate(this,R.layout.view4,null);
            list.add(view1);
            list.add(view2);
            list.add(view3);
            list.add(view4);


            //设置Adapter
            mVp.setAdapter(new WelcomeAdapter(list));

            mVp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int i, float v, int i1) {

                }

                @Override
                public void onPageSelected(int i) {
                    if (i==3){
                        mhandler.sendEmptyMessageDelayed(0,2000);
                    }
                }

                @Override
                public void onPageScrollStateChanged(int i) {

                }
            });


        }
    }



    private void setIntent(){
        startActivity(new Intent(WelcomeActivity.this,HomeActivity.class));
        finish();
    }
}
